import { NestFactory } from '@nestjs/core';
import { Logger } from '@nestjs/common';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import * as rateLimit from 'express-rate-limit';
import * as helmet from 'helmet';

async function bootstrap() {
    const app = await NestFactory.create(AppModule, {
        cors: true, // 设置跨站访问
        logger: false,
    });

    const options = new DocumentBuilder()
        .setTitle('东方新闻API')
        .setDescription('api')
        .setVersion('0.0.1')
        .addTag('cats')
        .addTag('dog')
        .build();
    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup('sw', app, document);

    // 访问频率限制
    app.use(
        rateLimit({
            windowMs: 15 * 60 * 1000, // 15分钟
            max: 100, // 限制15分钟内最多只能访问100次
        }),
    );

    // Web漏洞的
    app.use(helmet());

    await app.listen(3000, () => {
        Logger.log(`服务已经启动成功`);
    });
}

bootstrap().catch(e => Logger.error(e));
